﻿using AutoMapper;
using IOA.MES.Log.DataAccess.Entity;
using IOA.MES.Log.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IOA.MES.Log.Business
{
    /// <summary>
    /// 日志操作
    /// </summary>
    public class OprLogMgr
    {
        private MESLogContext logDb = new MESLogContext("MESLog");

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="models">日志对象</param>
        public void AddLogs(List<VOprLog> models)
        {
            models.ForEach(_ =>
            {
                _.CreateTime = DateTime.Now;
            });
            var entities = Mapper.Map<List<OprLog>>(models);
            logDb.OprLogs.AddRange(entities);
            logDb.SaveChanges();
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="model">日志对象</param>
        public void AddLog(VOprLog model)
        {
            model.CreateTime = DateTime.Now;
            logDb.OprLogs.Add(Mapper.Map<OprLog>(model));
            logDb.SaveChanges();
        }

        /// <summary>
        /// 查询操作日志
        /// </summary>
        /// <param name="subjectType">对象类型</param>
        /// <param name="subjectNo">对象编号</param>
        public List<VOprLog> QueryLogs(string subjectType, string subjectNo)
        {
            var entities = logDb.OprLogs.AsNoTracking().Where(_ => _.SubjectType == subjectType && _.SubjectNo == subjectNo && _.Valid).OrderBy(_ => _.PkId).ToList();
            return Mapper.Map<List<VOprLog>>(entities);
        }
    }
}
